<?php include("includes/session.php"); ?>
<?php include("includes/connect_db.php"); ?>
<?php include("includes/header.php"); ?>
<?php include("notif.php"); ?>

<style>
	.table-inbox{
		border: 1px solid #DDD; 
		font-family: 'SEGOE UI';
	}
	.tr-header{
		background-color: #0088cc; 
		color: #FFF;
	}
    .check{
        cursor: pointer;
    }
   	.table td {
   		padding: 5px;
   		border-top: 1px solid #EEE;	
   	}
</style>

<script type="text/javascript">
	/* Table initialisation */
	$(document).ready(function() {
		$('#example').dataTable( {
			"sDom": "<'row'<'span6'l><'span6'f>r>t<'row'<'span6'i><'span6'p>>",
			"sPaginationType": "bootstrap",
			"aoColumns": [
				{ "sClass": "center", "bSearchable": false, "bSortable": false },
				{ "sClass": "left", "bSearchable": true },
				{ "sClass": "left", "bSearchable": true, "bSortable": false },
				{ "sClass": "left", "bSortable": true, "bSearchable": false },
				{ "sClass": "left", "bSortable": false, "bSearchable": false }
			],
			"oLanguage": {
				"sLengthMenu": "_MENU_ records per page"
			}
		} )	
	} );
</script>

<div class="span9">  
    <h3>SMS</h3>                      
    <ul class="breadcrumb well">
        <li class="active">Outbox</li>                
    </ul>    
    
    <table class="table table-inbox" id="example">
        <thead>
            <tr class="tr-header">
                <th width="3%">
                    <input class="check-real-all" type="checkbox" style="display: none">
                    <i class="icon-check check-all" style="opacity:0.2"></i>
                </th>
                <th width="25%">
                	PENERIMA
                </th>
                <th width="48%">
                	PESAN
                </th>
                <th>
                	WAKTU
                </th>
                <th>
                </th>
            </tr> 
        </thead>
        <tbody>            
		
		<?php
            $sql_select_msg = "SELECT 
                                    a.ID, 
                                    a.DestinationNumber, 
                                    a.TextDecoded,
                                    a.Status,
                                    CONCAT(	DAY(a.SendingDateTime),' ',
                                            MONTHNAME(a.SendingDateTime),' ',
                                            YEAR(a.SendingDateTime),', ',
                                            DATE_FORMAT(a.SendingDateTime,'%h:%i %p')) 
                                    AS waktu 
                                FROM sentitems a                           
                                ORDER BY a.SendingDateTime DESC";

            $query_select_msg = mysql_query($sql_select_msg);
            
            $i = 1;
            while($baris_sent = mysql_fetch_array($query_select_msg)){
                echo "<tr class='select_msg'>";
        ?>
	        	<td>
	                <input class="id-msg" type="hidden" value="<?php echo $baris_sent['ID'];?>" />
	                <input class="check-real" type="checkbox" style="display:none">
	                <i class="icon-check check" style="opacity:0.2"></i>
				</td>
	            <td>
					<?php 
						$sql_cek_kontak = "SELECT * FROM tb_kontak_rincian where no_hp='$baris_sent[DestinationNumber]'";
						$query_cek_kontak = mysql_query($sql_cek_kontak);
						$banyak_row = mysql_num_rows($query_cek_kontak);

						if($banyak_row > 0){
							$query_cek_kontak = mysql_query($sql_cek_kontak);
							$cek_kontak = mysql_fetch_array($query_cek_kontak);
								$sql = "SELECT * FROM tb_kontak WHERE id_kontak = '$cek_kontak[id_kontak]'";
								$query = mysql_query($sql);
								$baris = mysql_fetch_array($query);

								echo substr($baris['nama_kontak'], 0, 20);

								if (strlen($baris['nama_kontak']) > 20) {
									echo "...";
								}
						} else{
							echo $baris_sent['DestinationNumber'];
						}
					?>
	         	</td>
	            <td class='view-msg' style='cursor: pointer'>
					<?php 
						if($baris_sent['TextDecoded']==''){ 
							echo "&nbsp";}
						else{
							echo substr($baris_sent['TextDecoded'], 0, 55);
							
							if (strlen($baris_sent['TextDecoded']) > 60){ 
								echo "...";
							} else{
								
							}
						}
					?>
	         	</td>
	            <td>
					<?php echo $baris_sent['waktu'];?>
				</td>
				<td>

					<?php
						if (($baris_sent['Status'] == "DeliveryFailed") or ($baris_sent['Status'] == "SendingError") or ($baris_sent['Status'] == "Error") ) {
							echo "<i class='icon-remove'></i>";
						} elseif (($baris_sent['Status'] == "DeliveryOK") or ($baris_sent['Status'] == "SendingOK") or ($baris_sent['Status'] == "SendingOKNoReport")) {
							echo "<i class='icon-ok'></i>";
						} else{
							echo "<i class='icon-time'></i>";
						}
					?>
				</td>
			</tr>
	<?php
                $i++;        
            }
	?>
    	</tbody>
	</table>

	<div class="pull-left" style="margin: -20px 0px 0px 0px">
    	<div class="input-prepend">
			<span class="add-on"><i class="icon-trash" style="opacity: 0.7;"></i></span>
			<button class="btn btn-danger but-del" type="button" disabled="disabled">Hapus</button>
		</div>
    </div>

</div><!--/span-->

<?php include("includes/footer.php"); ?>

<script type="text/javascript">
	var cek = 0;
	var tempView = '';
    $('document').ready(function(){
			$('.check').live('click',function(){
				if($(this).parent('td').find('.check-real').is(':checked')){
					cek--;
					$(this).parent('td').find('.check-real').removeAttr('checked');
					$(this).css('opacity','0.2');
					$(this).parent('td').parent('tr').removeClass('info');
					$(this).parent('td').parent('tr').css('color','');
				}else{
					cek++;
					$(this).parent('td').find('.check-real').attr('checked','checked');
					$(this).css('opacity','1');
					$(this).parent('td').parent('tr').addClass('info');
					$(this).parent('td').parent('tr').css('color','');
				}
				
				if(cek > 0){
					$('.but-del').removeAttr('disabled');
				} else{
					$('.but-del').attr('disabled','disabled');
				}
				
				if(cek==$('.check').length){
					$('.check-all').css('opacity','1');
					$('.check-real-all').attr('checked','checked');
				} else{
					$('.check-all').css('opacity','0.2');
					$('.check-real-all').removeAttr('checked');
				}
			});
			
			$('.check-all').click(function(){
				var cekCentang = $(this).parent('th').find('.check-real-all');
				//alert(cekCentang);
				if(cekCentang.is(':checked')){
					$('.check').each(function(){
						var temp = $(this);
						if($(this).parent('td').find('.check-real').is(':checked')){	
							temp.click();
						}
					});
					cekCentang.removeAttr('checked');
					$(this).css('opacity','0.2');
				}else{
					$('.check').each(function(){
						var temp = $(this);
						if(!$(this).parent('td').find('.check-real').is(':checked')){	
							temp.click();
						}
					});
					cekCentang.attr('checked','checked');
					$(this).css('opacity','1');
				}
			});
				
			$('.check-all').live({
				mouseenter:
				   function()
				   {
						$(this).css('opacity','1');
				   },
				mouseleave:
				   function()
				   {
						if(!$(this).parent('th').find('.check-real-all').is(':checked')){
							$(this).css('opacity','0.2');
						}		
				   }
			   }
			);

			
			$('.check').live({
				mouseenter:
				   function()
				   {
						$(this).css('opacity','1');
				   },
				mouseleave:
				   function()
				   {
						if(!$(this).parent('td').find('.check-real').is(':checked')){
							$(this).css('opacity','0.2');
						}		
				   }
			   }
			);

			$('.view-msg').live('click',function(){
				tempView = $(this);
	            $("#tampil-modal").fadeOut("medium",function(){
	                var form_pesan = {
	                    id_outbox: tempView.parent('tr').find('.id-msg').val()
	                };
	                $.ajax({ 
	                    type: "POST",
	                    url: "outbox-modal.php",
	                    data: form_pesan, 
	                    cache: false, 
	                    success: function(msg){ 
	                        $("#tampil-modal").html(msg);
	                        $("#tampil-modal").fadeIn("medium");
	                    } 
	                }); 
	            });
	            return false;
			});
			
			$('.but-del').click(function(){
				$("input:checkbox").each(function(){
					if($(this).is(':checked')){												
						var form_msg = {
							id_outbox: $(this).parent('td').find('.id-msg').val()
						};
						$.ajax({
							type: "POST",
							url: "act_outbox.php?aksi=3", 
							data: form_msg, 
							cache: false, 
							success: function(msg){
								setTimeout(function(){
									location.reload();  
								},500);
							} 
						});
					} 
				}); 
			});
      });
</script>